package com.ld.hpfs.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import com.ld.hpfs.dao.DealAbandonMapper;
import com.ld.hpfs.service.DealAbandonService;

@Service
public class DealAbandonServiceImpl implements DealAbandonService {
	
	private static final Logger logger = LoggerFactory.getLogger(DealServiceImpl.class);
	@Autowired
	private DealAbandonMapper dealAbandonMapper;

	@Autowired
	private JdbcTemplate jdbcTemplate;

	@Override
	public List<HashMap<String, Object>> getdealAbandonList(
			HashMap<String, Object> param) {
		if (param.get("VOID_FLAG") == null) {
			ArrayList<HashMap<String, Object>> checks = dealAbandonMapper
					.getCheckRights(param);
			if (checks != null && checks.size() > 0) {
				// List<HashMap<String,Integer>> status = new
				// ArrayList<HashMap<String,Integer>>();
				// for(HashMap<String, Object> check : checks){
				// int level =
				// Integer.parseInt(check.get("ABANDON_ROLE").toString());
				// HashMap<String,Integer> map= new HashMap<String,Integer>();
				// map.put("role", level);
				// status.add(map);
				// }
				param.put("status", checks);
			}
		}
		int count = dealAbandonMapper.getDealAbandonCount(param);
		if (count == 0) {
			return null;
		} else {
			param.put("pageCount", count);
			return dealAbandonMapper.getdealAbandonList(param);
		}
	}

	@Override
	public void dealAbandonInfoSave(HashMap<String, Object> param) {

		param.put("status", -1);
		dealAbandonMapper.dealAbandonInfoSave(param);

		// 清空状态参数
		param.remove("status");
		// 检查本票据类型的审核级数，根据级数更新审核状态
		ArrayList<HashMap<String, Object>> checkLevel = dealAbandonMapper
				.getCheckLevel(param);
		if (checkLevel != null && checkLevel.size() > 0) {
			if (checkLevel.get(0).get("CHECK_LEVEL") != null) {
				int level = Integer.parseInt(checkLevel.get(0)
						.get("CHECK_LEVEL").toString());
				if (level == 0) { // 无审核，直接通过
					param.put("status", 3);
					this.getTestInfo(param);
				} else {// 一级或二级审核，更新为等待一级审核
					param.put("status", 1);
				}
			} else {// 无记录，默认为无审核
				param.put("status", 3);
				this.getTestInfo(param);
			}
		} else {// 无记录，默认为无审核
			param.put("status", 3);
			this.getTestInfo(param);
		}
		// 更改红废状态
		 List<HashMap<String, Object>> invoide=dealAbandonMapper.getInoviceIdEnd();
		 Integer invoice_id=(Integer) invoide.get(0).get("voided_doc_id");
		 param.put("invoiceID", invoice_id);
		dealAbandonMapper.DealAbandonInfoupdate(param);
	}

	@Override
	public List<HashMap<String, Object>> getTestInfo(
			HashMap<String, Object> param) {
		// TODO Auto-generated method stub
		return dealAbandonMapper.getTestInfo(param);
	}

	@Override
	public void docTypeUpdate(HashMap<String, Object> param) {
		dealAbandonMapper.docTypeUpdate(param);
	}

	@Override
	public void deleteDealabandonByDocNo(HashMap<String, Object> param) {
		dealAbandonMapper.deleteDealabandonByDocNo(param);
	}

	@Override
	public void setDocStatus(HashMap<String, Object> param) {
		dealAbandonMapper.setDocStatus(param);
	}

	@Override
	public HashMap<String, Object> getInfoByDoc(HashMap<String, Object> param) {
		// TODO Auto-generated method stub
		return dealAbandonMapper.getInfoByDoc(param);
	}

	@Override
	public List<HashMap<String, Object>> getInfo(HashMap<String, Object> param) {
		// TODO Auto-generated method stub
		return dealAbandonMapper.getInfo(param);
	}

	@Override
	public void infoDelete(HashMap<String, Object> param) {
		dealAbandonMapper.infoDelete(param);
	}

	@Override
	public List<HashMap<String, Object>> getDocStatusInfo(
			HashMap<String, Object> param) {
		// TODO Auto-generated method stub
		return dealAbandonMapper.getDocStatusInfo(param);
	}

	@Override
	public List<HashMap<String, Object>> getInvoiceInfo(
			HashMap<String, Object> param) {
		// TODO Auto-generated method stub
		return dealAbandonMapper.getInvoiceInfo(param);
	}

	@Override
	public void updateInvoiceInfo(HashMap<String, Object> param) {
		dealAbandonMapper.updateInvoiceInfo(param);
	}

	@Override
	public List<HashMap<String, Object>> getCheckRights(
			HashMap<String, Object> param) {
		return dealAbandonMapper.getCheckRights(param);
	}

	@Override
	public HashMap<String, Object> getStatusByDoc(HashMap<String, Object> param) {
		return dealAbandonMapper.getStatusByDoc(param);
	}

	@Override
	public void updateStatus(HashMap<String, Object> param, boolean agree) {
		String[] voids = (String[]) param.get("voids");
		String[] docs = (String[]) param.get("docs");
		for (int i = 0; i < voids.length; i++) {
			HashMap<String, Object> updateParam = new HashMap<String, Object>();
			updateParam.put("VOIDED_DOC_ID", voids[i]);
			updateParam.put("DOCUMENT_NO", docs[i]);
			updateParam.put("companyId",
					Integer.parseInt(param.get("companyId").toString()));
			HashMap<String, Object> status = dealAbandonMapper
					.getStatusByDoc(updateParam);
			int previousStatus = Integer.parseInt(status.get("STATUS")
					.toString());

			// 用于更改TAX_D_ID_HEAD中的状态
			status.put("docNo", docs[i]);
			status.put("companyId",
					Integer.parseInt(param.get("companyId").toString()));
			status.put("update_id",
					Integer.parseInt(param.get("userId").toString()));
			status.put("userId",
					Integer.parseInt(param.get("userId").toString()));
			status.put("update_name", param.get("update_name").toString());

			if (previousStatus == 1) {
				updateParam.put("first",
						Integer.parseInt(param.get("userId").toString()));
			} else {
				updateParam.put("second",
						Integer.parseInt(param.get("userId").toString()));
			}
			if (!agree) { // 否决，审核状态改为0
				updateParam.put("status", 0);
			} else if (previousStatus == 2) { // 二级审核，审核状态改为3（通过）
				updateParam.put("status", 3);
				this.getTestInfo(status);
			} else { // 一级审核
						// 查看此票据类型的审核级数
				updateParam.put("docType", status.get("DOC_TYPE_CODE")
						.toString());
				ArrayList<HashMap<String, Object>> checkLevel = dealAbandonMapper
						.getCheckLevel(updateParam);
				if (checkLevel != null && checkLevel.size() > 0) {
					if (checkLevel.get(0).get("CHECK_LEVEL") != null) {
						int level = Integer.parseInt(checkLevel.get(0)
								.get("CHECK_LEVEL").toString());
						if (level == 0 || level == 1) { // 审核级数为无审核或一级审核，审核状态改为3（通过）
							updateParam.put("status", 3);
							this.getTestInfo(status);
						} else { // 审核级数为二级审核，审核状态改为2（等待二级审核）
							updateParam.put("status", 2);
						}
					} else { // 无记录，默认为无审核
						updateParam.put("status", 3);
						this.getTestInfo(status);
					}
				} else {// 无记录，默认为无审核
					updateParam.put("status", 3);
					this.getTestInfo(status);
				}
			}
			dealAbandonMapper.updateStatus(updateParam);
		}
	}
	/**
	 * 获取当前红废票状态
	 */
	@Override
	public HashMap<String, Object> getInvoiceStatus(HashMap<String, Object> param) {
		return dealAbandonMapper.getInvoiceStatus(param);
	}
	/**
	 * 获取所有一级审核者
	 */
	@Override
	public ArrayList<HashMap<String, Object>> getCheckLevel1(String cpnId) {
		// TODO Auto-generated method stub
		return dealAbandonMapper.getCheckLevel1(cpnId);
	}
	/**
	 * 获取所有二级审核者
	 */
	@Override
	public ArrayList<HashMap<String, Object>> getCheckLevel2(String cpnId) {
		// TODO Auto-generated method stub
		return dealAbandonMapper.getCheckLevel2(cpnId);
	}
	/**
	 * 获取申请者信息
	 */
	@Override
	public HashMap<String, Object> getMsgForApplicant(String userId) {
		// TODO Auto-generated method stub
		return dealAbandonMapper.getMsgForApplicant(userId);
	}

	@Override
	public ArrayList<HashMap<String, Object>> getAbandonType(
			HashMap<String, Object> param) {
		// TODO Auto-generated method stub
		return dealAbandonMapper.getAbandonType(param);
	}

	@Override
	public int getLastInsert(HashMap<String, Object> param) {
		// TODO Auto-generated method stub
		return dealAbandonMapper.getLastInsert(param);
	}

	@Override
	public void updateBaseOnSp(HashMap<String, Object> param) {
		// TODO Auto-generated method stub
		dealAbandonMapper.updateBaseOnSp(param);
	}

	@Override
	public HashMap<String, Object> getAbandonCount(HashMap<String, Object> param) {
		// TODO Auto-generated method stub
		return dealAbandonMapper.getAbandonCount(param);
	}
	// 红废一般查询单据号列表
	@Override
	public ArrayList<HashMap<String, Object>> getAbandonNo(
			HashMap<String, Object> param) {
		// TODO Auto-generated method stub
		return dealAbandonMapper.getAbandonNo(param);
	}
	// 红废一般查询客户列表
	@Override
	public ArrayList<HashMap<String, Object>> getDealAbandonCust(
			HashMap<String, Object> param) {
		// TODO Auto-generated method stub
		return dealAbandonMapper.getDealAbandonCust(param);
	}
	// 更新红废票
	@Override
	public void dealAbandonUpdate(HashMap<String, Object> param) {
		// TODO Auto-generated method stub
		dealAbandonMapper.dealAbandonUpdate(param);
	}

	@Override
	public ArrayList<HashMap<String, Object>> checkEditRight(HashMap<String, Object> param) {
		// TODO Auto-generated method stub
		return dealAbandonMapper.checkEditRight(param);
	}

	@Override
	public int getAandonStatus(HashMap<String, Object> param) {
		// TODO Auto-generated method stub
		return dealAbandonMapper.getAandonStatus(param);
	}

	@Override
	public ArrayList<HashMap<String,Object>> getInvoiceNumBegin(HashMap<String, Object> param) {
		try {
			return dealAbandonMapper.getInvoiceNumBegin(param);
		} catch (Exception ex) {
			 logger.error(ex.getMessage());
			 return null;
		}
		
	}

	@Override
	public ArrayList<HashMap<String,Object>> getInvoiceNumEnd(HashMap<String, Object> param) {
		try {
			return dealAbandonMapper.getInvoiceNumEnd(param);
		} catch (Exception ex) {
			 logger.error(ex.getMessage());
			 return null;
		}
		
	}
	
	@Override
	public ArrayList<HashMap<String,Object>> getInvoiceCodetoAbandon(HashMap<String, Object> param) {
		try {
			return dealAbandonMapper.getInvoiceCodetoAbandon(param);
		} catch (Exception ex) {
			 logger.error(ex.getMessage());
			 return null;
		}
		
	}
	
	
	@Override
	public ArrayList<HashMap<String,Object>> getInoviceIdEnd() {
		try {
			return dealAbandonMapper.getInoviceIdEnd();
		} catch (Exception ex) {
			 logger.error(ex.getMessage());
			 return null;
		}
		
	}
	
	
	@Override
	public ArrayList<HashMap<String,Object>> getRoleById(HashMap<String, Object> param) {
		try {
			return dealAbandonMapper.getRoleById(param);
		} catch (Exception ex) {
			 logger.error(ex.getMessage());
			 return null;
		}
		
	}
	
}
